package com.cskaoyan.wordmemorize.controller.app;

import com.cskaoyan.wordmemorize.auth.StpKit;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.app.UserCheckinDTO;
import com.cskaoyan.wordmemorize.dto.app.UserCheckinStatisticsDTO;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.UserCheckinCommand;
import com.cskaoyan.wordmemorize.service.UserCheckinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("user/checkin")
public class UserCheckinController {
    @Autowired
    UserCheckinService userCheckinService;

    @GetMapping("statistics")
    public Result<UserCheckinStatisticsDTO> getCheckinStatus() {
        Long userId = StpKit.USER.getLoginIdAsLong();
        UserCheckinStatisticsDTO userCheckinStatistics = userCheckinService.getUserCheckinStatistics(userId);
        return Result.ok(userCheckinStatistics);
    }

    @GetMapping("list")
    public Result<PageDTO<UserCheckinDTO>> getCheckinList(PageRequest request) {
        Long userId = StpKit.USER.getLoginIdAsLong();
        PageDTO<UserCheckinDTO> userCheckinDTOPageDTO = userCheckinService.getCheckInData(request,userId);
        return Result.ok(userCheckinDTOPageDTO);
    }

    @PostMapping("")
    public Result<UserCheckinStatisticsDTO> checkin(@RequestBody UserCheckinCommand userCheckinCommand) {
        Long userId = StpKit.USER.getLoginIdAsLong();
        userCheckinCommand.setUserId(String.valueOf(userId));
        UserCheckinStatisticsDTO userCheckinDTO = userCheckinService.checkin(userCheckinCommand);
        return Result.ok(userCheckinDTO);
    }

    @PutMapping("card")
    public Result userResignCard() {
        Long userId = StpKit.USER.getLoginIdAsLong();
        userCheckinService.userResignCard(userId);
        return Result.ok();
    }
}
